﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Xml.Serialization;

namespace DeepHash.Document.Format
{
    /// <summary>
    /// Formats the document as xml. Basically it calls the xml serializer to serialize 
    /// and then to deserialize
    /// </summary>
    public class XmlDocumentFormatter : IDocumentFormatter
    {
        #region IDocumentFormatter Members

        public bool SaveDocument(System.IO.Stream stream, VerificationDocument doc)
        {
            List<Type> types = new List<Type>();
            types.Add(typeof(VerificationRecord));
            types.Add(typeof(FolderVerificationRecord));
            types.Add(typeof(FileVerificationRecord));    

            XmlSerializer serializer = new XmlSerializer(typeof(VerificationDocument), types.ToArray());
            serializer.Serialize(stream, doc);
            return true;
        }

        public VerificationDocument LoadDocument(System.IO.Stream stream)
        {
            List<Type> types = new List<Type>();
            types.Add(typeof(FolderVerificationRecord));
            types.Add(typeof(FileVerificationRecord));

            XmlSerializer serializer = new XmlSerializer(typeof(VerificationDocument), types.ToArray());
            return serializer.Deserialize(stream) as VerificationDocument;            
        }

        #endregion
    }
}
